package com.cd2cd.code_template.configuration.security;

import com.alibaba.fastjson.annotation.JSONField;
import com.cd2cd.code_template.configuration.security.loginUserService.LoadUserService.LoginTypeUser;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 登录用户信息
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TokenUser extends User {

    private static final long serialVersionUID = 1L;

    private Class<? extends LoginTypeUser> type;

    private Integer userId;

    public TokenUser(Integer userId, String username, String password, Class<? extends LoginTypeUser> type, Set<GrantedAuthority> authorities) {
        super(username, password, authorities);
        this.type = type;
        this.userId = userId;
    }

    @JSONField(serialize = false)
    public Set<String> getAuthIds() {
        Collection<GrantedAuthority> authRoles = this.getAuthorities();
        if (authRoles != null && authRoles.size() > 0) {
            Set<String> authIds = new HashSet<>();
            authRoles.stream().forEach(aa -> {
                String role = aa.getAuthority();
                authIds.add(role);
            });
            return authIds;
        }
        return null;
    }

    @JSONField(serialize = false)
    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return super.getAuthorities();
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return super.isCredentialsNonExpired();
    }

    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return super.isAccountNonExpired();
    }

    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return super.isAccountNonLocked();
    }

    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return super.isEnabled();
    }

    public String getType() {
        return type.getSimpleName();
    }

    public void setType(Class<? extends LoginTypeUser> type) {
        this.type = type;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
}
